data_4 <- read.csv("C:/Users/cparr/Downloads/wave 4_test.csv")

data_5 <- read.csv("C:/Users/cparr/Downloads/wave 5_test.csv")

library(mice)


imputed <- mice(data_4, m = 80, method = 'pmm', seed = 123)


completed_df <- complete(imputed, 1)


write.csv(completed_df, "imputed_data_4.csv", row.names = FALSE)


imputed <- mice(data_5, m = 40, method = 'pmm', seed = 123)


completed_df <- complete(imputed, 1)

write.csv(completed_df, "imputed_data_4.csv", row.names = FALSE)


library(mice)


vars_to_impute <- c(
  "executive", "courts", "national", "parties", "parliament",
  "civil", "military", "police", "local", "newspaper", "tv",
  "election", "ngos", "neighbors", "others", 
  "social_media", "info", "express"
)


data_subset <- data_5[vars_to_impute]


pred <- quickpred(data_subset, mincor = 0.05)


imputed <- mice(data_subset, m = 40, method = 'pmm', predictorMatrix = pred, ridge = 1e-4, seed = 123)

completed_data <- complete(imputed, action = "long", include = TRUE)


non_imputed_vars <- setdiff(names(data_5), vars_to_impute)
data_non_imputed <- data_5[non_imputed_vars]


imputed_data_m1 <- complete(imputed, 1)  # first imputed dataset


final_data <- cbind(imputed_data_m1, data_non_imputed)

write.csv(final_data, "imputed_data_5.csv", row.names = FALSE)


